package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Health_report_dao;
import com.clinic.domain.Health_report;
import com.clinic.util.Util_imp;

public class Health_report_imp implements Health_report_dao {

	SessionFactory session_factory = null;

	public Health_report_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_health_report(Health_report ht) {
		System.out.println("Enter - create_health_report");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int ht_obj_id = 0;
		try {
			tx = session.beginTransaction();
			ht_obj_id = (Integer) session.save(ht);
			tx.commit();
			System.out.println("New Health_report Created with ID: "
					+ ht_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Health_report Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_health_report");
		return ht_obj_id;
	}

	@Override
	public void update_health_report(Health_report ht) {
		System.out.println("Enter - update_health_report");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(ht);
			tx.commit();
			System.out.println("Health_report Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Health_report Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_health_report");
	}

	@Override
	public void delete_health_report(int ht_obj_id) {
		System.out.println("Enter - delete_health_report");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Health_report ht = (Health_report) session.get(Health_report.class,	ht_obj_id);
			session.delete(ht);
			tx.commit();
			System.out.println("Health_report with ID: " + ht_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Health_report Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_health_report");
	}

	@Override
	public Health_report get_health_report(int ht_obj_id) {
		System.out.println("Enter - get_health_report");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Health_report ht = null;
		try {
			tx = session.beginTransaction();
			ht = (Health_report) session.createQuery("from Health_report where ht_obj_id=:id")
					.setParameter("id", ht_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Owner Name: " + ht.getObj_owner_name());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_health_report");
		return ht;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Health_report> get_all_health_reports() {
		System.out.println("Enter - get_all_health_reports");
		Session session = session_factory.openSession();
		List<Health_report> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Health_report").list();
			tx.commit();
			System.out.println("Number of Health_reports returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_health_reports");
		return l;
	}

	@Override
	public Health_report persistOrMerge(Health_report ht) {
		return (Health_report) session_factory.getCurrentSession().merge(ht);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Health_report").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Health_report: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}